package org.me.Slide.Util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.me.Slide.data.Albums;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.util.Log;

public class XMLUtil {
	
	private static InputStream mInputStream 						= null;
	private static DocumentBuilderFactory mDocumentBuilderFactory 	= null;
	private static DocumentBuilder mDocumentBuilder 				= null;
	private static Document mDocument								= null;
	
	//parse data tu file xml vao List<Albums>
	
	public static List<Albums> ParseDataFromXMLToList(Context context)
	{
		try
		{
			mInputStream = context.getAssets().open("xml/albumslist.xml");
			
			mDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
			
			mDocumentBuilder = mDocumentBuilderFactory.newDocumentBuilder();
			
			mDocument = mDocumentBuilder.parse(mInputStream);
			
			mDocument.getDocumentElement().normalize();
			
			NodeList mNodeList = mDocument.getElementsByTagName("items");
			
			List<Albums> mAlbums = new ArrayList<Albums>();
			
			for(int i = 0; i< mNodeList.getLength(); i++)
			{
				Node mNode = mNodeList.item(i);
				
				if(mNode.getNodeType() == Node.ELEMENT_NODE)
				{
					//id
					Element elementID 		= (Element) mNode;
					NodeList nodeID 		= elementID.getElementsByTagName("id");
					Element ID 				= (Element) nodeID.item(0);
	      	    	NodeList nodelistID 	= ID.getChildNodes();
	      	    	
	      	    	//titile
	      	    	Element elementtitle	= (Element) mNode;
					NodeList nodetitle 	= elementtitle.getElementsByTagName("title");
					Element title 			= (Element) nodetitle.item(0);
	      	    	NodeList nodelistTitle 	= title.getChildNodes();
	      	    	
	      	    	//image
	      	        Element imagesElement 	= (Element) mNode;
	    	    	NodeList noteImage 		= imagesElement.getElementsByTagName("image");
	    	    	Element image 			= (Element) noteImage.item(0);
	    	    	NodeList nodelistImage 	= image.getChildNodes();
	      	    	
	    	    	String valueID 		= ((Node) nodelistID.item(0)).getNodeValue();
	    	        String valueTitle 	= ((Node) nodelistTitle.item(0)).getNodeValue();
	    	        String imagesName 	= ((Node) nodelistImage.item(0)).getNodeValue();
	      	    	
	      	    	mAlbums.add(new Albums(valueID, valueTitle, imagesName));
	      	    	
				}
			}
			
			return mAlbums;
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	
	//parse data tu file xml/asianhost vao grid view
	
	public static List<Albums> parseDataFromXMLtoGridView(Context context, String filename)
	{
		try
		{
			mInputStream = context.getAssets().open("xml/"+filename+".xml");
			
			mDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
			
			mDocumentBuilder = mDocumentBuilderFactory.newDocumentBuilder();
			
			mDocument = mDocumentBuilder.parse(mInputStream);
			
			mDocument.getDocumentElement().normalize();
			
			NodeList mNodeList = mDocument.getElementsByTagName("items");
			
			List<Albums> mAlbums = new ArrayList<Albums>();
			
			for(int i = 0; i< mNodeList.getLength(); i++)
			{
				Node mNode = mNodeList.item(i);
				
				if(mNode.getNodeType() == Node.ELEMENT_NODE)
				{
					//titile
	      	    	Element elementtitle	= (Element) mNode;
					NodeList nodetitle 	= elementtitle.getElementsByTagName("title");
					Element title 			= (Element) nodetitle.item(0);
	      	    	NodeList nodelistTitle 	= title.getChildNodes();
	      	    	
	      	    	//image
	      	        Element imagesElement 	= (Element) mNode;
	    	    	NodeList noteImage 		= imagesElement.getElementsByTagName("image");
	    	    	Element image 			= (Element) noteImage.item(0);
	    	    	NodeList nodelistImage 	= image.getChildNodes();
	      	    	
	    	    	
	    	        String valueTitle 	= ((Node) nodelistTitle.item(0)).getNodeValue();
	    	        String imagesName 	= ((Node) nodelistImage.item(0)).getNodeValue();
	      	    	
	      	    	mAlbums.add(new Albums(valueTitle, imagesName));
	      	    	
				}
			}
			
			return mAlbums;
			
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	
	

}
